package com.fanix5.javamvvm.utils;

import android.text.TextUtils;
import android.text.format.Time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * FileName: TimeUtils
 * Author: Admin
 * Date: 2020/11/14 9:18
 * Description: 时间工具类
 */
public class TimeUtils {

    private static final String ISO_8601 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final String MM_DD = "MM月dd日";
    private static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final String YYYY_MM_DD_HH_MM_SS2 = "yyyy/MM/dd HH:mm:ss";

    private static final Time localTime = new Time("Asia/Hong_Kong");

    public static long getStamp() {
        return Calendar.getInstance().getTimeInMillis() / 1000;
    }

    public static long getStampAll() {
        return Calendar.getInstance().getTimeInMillis();
    }

    public static String getAll() {
        localTime.setToNow();
        return localTime.format("%Y-%m-%d %H:%M:%S");
    }

    public static String getNoChar() {
        localTime.setToNow();
        return localTime.format("%Y%m%d%H%M%S");
    }

    public static String getDate() {
        localTime.setToNow();
        return localTime.format("%Y-%m-%d");
    }

    public static String getTime() {
        localTime.setToNow();
        return localTime.format("%H:%M:%S");
    }

    public static String getYear() {
        localTime.setToNow();
        return localTime.format("%Y");
    }

    public static String getMouth() {
        localTime.setToNow();
        return localTime.format("%m");
    }

    public static String getDay() {
        localTime.setToNow();
        return localTime.format("%d");
    }

    public static String getHour() {
        localTime.setToNow();
        return localTime.format("%H");
    }

    public static String getMinute() {
        localTime.setToNow();
        return localTime.format("%M");
    }

    public static String getSeconds() {
        localTime.setToNow();
        return localTime.format("%S");
    }

    public static String stamp2Time(long j) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).format(new Date(j * 1000));
    }

    public static String getWeek() {
        return "";
    }

    public static String time2Stamp(String str) {
        try {
            return String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).parse(str).getTime() / 1000);
        } catch (Exception unused) {
            return "";
        }
    }

    /**
     * 通过时间戳获取格式化时间
     *
     * @param dateFormat 格式
     * @param date       时间
     * @return 字符串
     */
    public static String formatDate(String dateFormat, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.CHINESE);
        return sdf.format(date);
    }

    /**
     * 通过字符串获取Date
     *
     * @param dateFormat 格式
     * @param dateString 时间字符串
     * @return 返回 dateString 对应的 Date
     */
    public static Date getDateFromString(String dateFormat, String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.getDefault());
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDateFromString(String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS, Locale.getDefault());
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDateFromString2(String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS2, Locale.getDefault());
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String timeStampToTimeString(String timeStampString) {
        if (TextUtils.isEmpty(timeStampString)) {
            return "2020-01-01 00:00:00";
        }
        Date date = getDateFromString(YYYY_MM_DD_HH_MM_SS_SSS, timeStampString);
        return formatDate(YYYY_MM_DD_HH_MM_SS, date);
    }

    public static String timeStampToDateString(String timeStampString) {
        if (TextUtils.isEmpty(timeStampString)) {
            return "2020-01-01";
        }
        Date date = getDateFromString(YYYY_MM_DD_HH_MM_SS_SSS, timeStampString);
        return formatDate(YYYY_MM_DD, date);
    }

    /**
     * 将一个时间转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param timeStamp 时间
     * @return String
     */
    public static String convertTimeToFormat(long timeStamp) {
        long curTime = System.currentTimeMillis() / (long) 1000;
        long time = curTime - timeStamp;

        if (time < 60 && time >= 0) {
            return "刚刚";
        } else if (time >= 60 && time < 3600) {
            return time / 60 + "分钟前";
        } else if (time >= 3600 && time < 3600 * 24) {
            return time / 3600 + "小时前";
        } else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
            return time / 3600 / 24 + "天前";
        } else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 + "个月前";
        } else if (time >= 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 / 12 + "年前";
        } else {
            return "刚刚";
        }
    }

    public static String getCurrentTime(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        return simpleDateFormat.format(new Date());
    }

    public static boolean judgmentDate(String tableTime) {
        String currentTime = getCurrentTime(YYYY_MM_DD_HH_MM_SS);//当前时间
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS, Locale.getDefault());
        Date start = null;//业务时间
        Date end = null;//当前时间
        try {
            start = sdf.parse(tableTime);
            end = sdf.parse(currentTime);
            long cha = end.getTime() - start.getTime();
            if (cha < 0) {
                return false;
            }
            double result = cha * 1.0 / (1000 * 60 * 60);
            return result <= 24;//是小于等于 hour 小时
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean judgmentDate(Date start) {
        String currentTime = getCurrentTime(YYYY_MM_DD_HH_MM_SS);//当前时间
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS, Locale.getDefault());
        Date end = null;//当前时间
        try {
            end = sdf.parse(currentTime);
            long cha = end.getTime() - start.getTime();
            if (cha < 0) {
                return false;
            }
            double result = cha * 1.0 / (1000 * 60 * 60);
            return result <= 24;//是小于等于 hour 小时
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getChineseDate(String dayTime) {
        if (TextUtils.isEmpty(dayTime)) {
            return "1月1日";
        }
        Date date = getDateFromString(YYYY_MM_DD_HH_MM_SS_SSS, dayTime);
        return formatDate(MM_DD, date);
    }

    /**
     * 时间差 天数
     */

    public static String getDatePoor(Date endDate, Date nowDate) {
        try {
            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            // long ns = 1000;
            // 获得两个时间的毫秒时间差异
            long diff = endDate.getTime() - nowDate.getTime();
            // 计算差多少天
            long day = diff / nd;
            // 计算差多少小时
            long hour = diff % nd / nh;
            // 计算差多少分钟
            long min = diff % nd % nh / nm;
            // 计算差多少秒//输出结果
            // long sec = diff % nd % nh % nm / ns;
            return day + "天" + hour + "小时" + min + "分钟";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 时间差毫秒数
     */

    public static long getDateDiff(Date endDate, Date nowDate) {
        try {
            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            // long ns = 1000;
            // 获得两个时间的毫秒时间差异
            long diff = endDate.getTime() - nowDate.getTime();
            // 计算差多少天
            long day = diff / nd;
            // 计算差多少小时
            long hour = diff % nd / nh;
            // 计算差多少分钟
            long min = diff % nd % nh / nm;
            // 计算差多少秒//输出结果
            // long sec = diff % nd % nh % nm / ns;
            return diff;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    /**
     * 将毫秒转成时分秒
     *
     * @param time
     * @return
     */

    public static String getGapTime(long time) {
        long hours = time / (1000 * 60 * 60);
        long minutes = (time - hours * (1000 * 60 * 60)) / (1000 * 60);
        long second = (time - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
        String diffTime = "";
        String hourStr = "";
        if (hours < 10) {
            hourStr = hours + "0";
        } else {
            hourStr = hours + "";
        }
        if (minutes < 10) {
            diffTime = hourStr + ":0" + minutes;
        } else {
            diffTime = hourStr + ":" + minutes;
        }
        if (second < 10) {
            diffTime = diffTime + ":0" + second;
        } else {
            diffTime = diffTime + ":" + second;
        }

        return diffTime;
    }

}